// pages/HomePage/HomePage.ts
Page({

  /**
   * 页面的初始数据 => 响应式数据仓库
   */
  data: {
    isShowSearch:true,
    types:[
      { title:'猜你喜欢',list:[], key: 'suggestion' },
      { title:'影院热映',list:[], key: 'movie_showing' },
      { title:'近期热门',list:[], key: 'movie_latest' }
    ],
    source:2
  },
  // 小程序2.1以前的版本，直接在Page方法内定义方法，即可作为页面或逻辑公用方法
  changeState(event:any){
    this.setData({
      isShowSearch:event.target?.dataset?.state ?? true,
    })
  },
  goMore(e:any){
    const key = e.currentTarget?.dataset?.key || 'movie_weekly_best';
    try {
      const app = getApp<IAppOption>();
      if (app.globalData) {
        (app.globalData as any).pendingRankKey = key;
      }
    } catch (_) {}
    wx.switchTab({ url: '/pages/ListPage/ListPage' });
  },
  // 猜你喜欢
  initLoadSuggestion(){
    wx.request({
      url:"https://m.douban.com/rexxar/api/v2/movie/suggestion",
      method:"GET",
      data:{
        start:0,
        count:10,
        new_struct:1,
        with_review:1,
        for_mobile:1,
        ck:"sew4"
      },
      success:(result)=>{
        const raw = (result as any)?.data?.items ?? [];
        const list = raw.map((it:any)=>({
          ...it,
          cover_url: it.cover_url || it.cover?.url || '',
          rating: it.rating || { value: 0 }
        }));
        this.setData({
          "types[0].list": list
        })
      },
      fail:(error)=>{
        console.log(error);
      }
    })
  },
  // 影院热映
  initLoadShowing(){
    wx.request({
      url: "https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items",
      method: "GET",
      data: {
        start: 0,
        count: 10,
        for_mobile: 1,
        ck: "sew4"
      },
      success: (result) => {
        const raw = (result as any)?.data?.subject_collection_items ?? [];
        const list = raw.map((it:any)=>({
          ...it,
          cover_url: it.cover_url || it.cover?.url || '',
          rating: it.rating || { value: 0 }
        }));
        this.setData({
          "types[1].list": list
        })
      },
      fail: (error) => {
        console.log(error);
      }
    })
  },
  // 近期热门
  initLoadLatest(){
    wx.request({
      url: "https://m.douban.com/rexxar/api/v2/subject_collection/movie_latest/items",
      method: "GET",
      data: {
        start: 0,
        count: 10,
        for_mobile: 1,
        ck: "sew4"
      },
      success: (result) => {
        const raw = (result as any)?.data?.subject_collection_items ?? [];
        const list = raw.map((it:any)=>({
          ...it,
          cover_url: it.cover_url || it.cover?.url || '',
          rating: it.rating || { value: 0 }
        }));
        this.setData({
          "types[2].list": list
        })
      },
      fail: (error) => {
        console.log(error);
      }
    })
  },
  // 生命周期-小程序页面创建完成-只会执行一次
  onLoad(){
    // 首页三个分区并行加载
    this.initLoadSuggestion();
    this.initLoadShowing();
    this.initLoadLatest();
    // 背景音乐（可选）
    try {
      const bgm = wx.getBackgroundAudioManager?.();
      if (bgm) {
        bgm.title = "坐在这里";
        bgm.epname = "首页BGM";
        bgm.singer = "unknown";
        const src = "https://vt1.doubanio.com/audio/ta_zuo_zai_na_li.mp3";
        bgm.src = src;
        // 循环播放处理
        if (typeof bgm.onEnded === 'function') {
          bgm.onEnded(() => {
            try { bgm.src = src; } catch (_) {}
          });
        }
      }
    } catch (e) {
      // 忽略BGM异常
    }
  }
})